package my.jdbc;

import java.sql.Connection;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import transaction.TransactionCallBack;

public class JdbcTemplate {
	public static final String SQL_REGEX = "\\[(.*?)\\]";

	/**
	 * 根据fields来查询数据
	 * 
	 * @param sql
	 * @param c
	 * @return List<T>
	 */
	public static <T> List<T> queryListInfoByFields(Connection con, String sql, Class<T> c, List<Object> fields, String typeName) {
		return JdbcUtil.buildSqlForListBean(con, sql, c, fields, typeName);
	}

	/**
	 * 根据对象bean来查询数据
	 * 
	 * @param sql
	 * @param c
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> queryListInfoByBean(Connection con, String sql, Class<T> c, Object bean, String typeName) {
		Matcher matcher = Pattern.compile(JdbcTemplate.SQL_REGEX).matcher(sql);
		List<Object> fields = JdbcUtil.formatBeanToFields(bean, matcher);
		sql = matcher.replaceAll("?");
		return JdbcUtil.buildSqlForListBean(con, sql, c, fields, typeName);
	}

	public static <T> T queryObjectInfoByFields(Connection con, String sql, Class<T> c, List<Object> fields, String typeName) {
		List<T> list = queryListInfoByFields(con, sql, c, fields, typeName);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public static <T> T queryObjectInfoByBean(Connection con, String sql, Class<T> c, Object bean, String typeName) {
		List<T> list = queryListInfoByBean(con, sql, c, bean, typeName);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public static List<Object[]> queryListFieldInfoByFields(Connection con, String sql, List<Object> fields, String typeName) {
		return JdbcUtil.buildSqlForListField(con, sql, fields, typeName);
	}

	public static Vector<Object> queryVectorFieldInfoByFields(Connection con, String sql, List<Object> fields, String typeName) {
		return JdbcUtil.buildSqlForVectorField(con, sql, fields, typeName);
	}

	// 得到所有的列名
	public static Vector<String> getTableColumnNameBySql(Connection con, String sql, String typeName) {
		return JdbcUtil.getTableColumnNameBySql(con, sql, typeName);
	}

	public static <T> List<Object[]> queryListFieldInfoByBean(Connection con, String sql, Object bean, String typeName) {
		Matcher matcher = Pattern.compile(JdbcTemplate.SQL_REGEX).matcher(sql);
		List<Object> fields = JdbcUtil.formatBeanToFields(bean, matcher);
		sql = matcher.replaceAll("?");
		return JdbcUtil.buildSqlForListField(con, sql, fields, typeName);
	}

	public static Object[] queryFieldInfoByFields(Connection con, String sql, List<Object> fields, String typeName) {
		List<Object[]> list = JdbcUtil.buildSqlForListField(con, sql, fields, typeName);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public static <T> Object[] queryFieldInfoByBean(Connection con, String sql, Object bean, String typeName) {
		List<Object[]> list = queryListFieldInfoByBean(con, sql, bean, typeName);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public static boolean updateByBean(Connection con, String sql, Object bean, String typeName) {
		Matcher matcher = Pattern.compile(JdbcTemplate.SQL_REGEX).matcher(sql);
		List<Object> fields = JdbcUtil.formatBeanToFields(bean, matcher);
		sql = matcher.replaceAll("?");
		return JdbcUtil.executeUpdate(con, sql, fields, typeName);
	}

	public static boolean updateByFields(Connection con, String sql, List<Object> fields, String typeName) {
		return JdbcUtil.executeUpdate(con, sql, fields, typeName);
	}

	public static boolean insertByBean(Connection con, String sql, Object bean, String typeName) {
		Matcher matcher = Pattern.compile(JdbcTemplate.SQL_REGEX).matcher(sql);
		List<Object> fields = JdbcUtil.formatBeanToFields(bean, matcher);
		sql = matcher.replaceAll("?");
		return JdbcUtil.executeUpdate(con, sql, fields, typeName);
	}

	public static boolean insertByFields(Connection con, String sql, List<Object> fields, String typeName) {
		return JdbcUtil.executeUpdate(con, sql, fields, typeName);
	}

	/**
	 * 执行JDBC事务操作
	 */
	public static void executeJDBCTransactionOperation(Connection con, TransactionCallBack tc, String typeName) {
		JdbcUtil.beginJdbcTransaction(con, tc, typeName);
	}

	public static void main(String[] args) {

	}
}
